package com.monicaca.controller;


import com.monicaca.bean.INV;
import com.monicaca.bean.WLK;
import com.monicaca.bean.INV_LOG;
import com.monicaca.bean.RKD;
import com.monicaca.service.INVService;
import com.monicaca.service.WLKService;
import com.monicaca.service.INV_LOGService;
import com.monicaca.service.RKDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/RKD")
public class RKDController {
    @Autowired
    RKDService rkdService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    INVService invService;
    @Autowired
    INV_LOGService inv_logService;
    @Autowired
    WLKService wlkService;

   
    @GetMapping("/")
    public String indexView(
            Model model){
        model.addAttribute("RKD",rkdService.findAll());
        return "RKD";
    }
    
    @PostMapping("/update/{id}")
    public String updateView(
            Model model, @PathVariable int id,RKD rkd){
        RKD realuser=rkdService.findByRKDId(id); 
        realuser.setSL(rkd.getSL());        
        rkdService.updateRKD(realuser);
        model.addAttribute("msg","修改成功");
        model.addAttribute("RKD",rkdService.findAll());
        return "RKD";
    }
    
    
    
    @GetMapping("/up/{cid}")
    public String up(Model model, @PathVariable int cid){
		RKD rkd=rkdService.findByRKDId(cid);
		if(rkd.getZT().equals("完成")==true){
			model.addAttribute("RKD",rkdService.findAll());
    		model.addAttribute("msg","该入库单已提交过，不要重复提交！"); 
    		return "RKD";			
		}
		
		
		
		rkd.setZT("完成");
		rkdService.updateRKD(rkd);
		
		int id=rkd.getId();
		WLK wl=wlkService.findByRKDId(id);
		
		INV reluser=invService.findByInvId(id);
    	if(reluser==null){
    		INV inv=new INV();
    		inv.setId(rkd.getId());
    		inv.setNum(rkd.getSL());
    		inv.setWL(rkd.getWL());
    		inv.setDW(rkd.getDW());
    		invService.createINV(inv);
    		model.addAttribute("RKD",rkdService.findAll());
    		model.addAttribute("msg","提交成功"); 
    		
    		INV_LOG inv_log=new INV_LOG();
            inv_log.setId(rkd.getId());
            inv_log.setNum(rkd.getSL());
            inv_log.setWL(rkd.getWL());
            inv_log.setDW(rkd.getDW());
            inv_log.setAction("增加");	
    		inv_logService.createINV_LOG(inv_log);
    		
  
    		
    		return "RKD";
    	}
    	else{
    		reluser.setNum(reluser.getNum()+rkd.getSL());
    		invService.updateINV(reluser);
    	}
    	
    	INV_LOG inv_log1=new INV_LOG();
        
        inv_log1.setId(rkd.getId()); 
        inv_log1.setNum(rkd.getSL());
        inv_log1.setWL(wl.getWL());
        inv_log1.setDW(wl.getDW());
        inv_log1.setAction("增加");
        
         inv_logService.createINV_LOG(inv_log1);
         model.addAttribute("RKD",rkdService.findAll());

    	return "RKD";   
    }


    
    
	@PostMapping("/save")
    public String updateView2(Model model, RKD rkd){
	    
		rkdService.createRKD(rkd);
		
		Date day=new Date();    
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		
		int id=rkd.getId();
    	rkd.setTime(df.format(day));
    	
    	rkd.setZT("初始");
    	
    	WLK wl=wlkService.findByRKDId(id);
 	
    	if(wl==null){
    		rkdService.deleteRKD(rkd);
    	
    		return "message";
    	}
    		
    	rkd.setDW(wl.getDW());
    	rkd.setWL(wl.getWL());
    	rkdService.updateRKD(rkd);
    	
       
             model.addAttribute("RKD",rkdService.findAll());
       
        return "RKD";
    }
 
	
    
    @GetMapping("/add")
    public String addView(
            Model model){
        return "RKDadd";
    }
    @GetMapping("/delete/{cid}")
    public String deteleView(
            Model model, @PathVariable int cid){


    	
    	RKD rkd=rkdService.findByRKDId(cid);
    	String ZT=rkd.getZT();
    	if(ZT.equals("完成")==true){
    		 model.addAttribute("msg","出库单已完成，不能删除！");
    	        model.addAttribute("RKD",rkdService.findAll());
    	        return "RKD";
    	} 	
        rkdService.deleteRKD(cid);
        model.addAttribute("msg","删除成功");
        model.addAttribute("RKD",rkdService.findAll());
        return "RKD";
    }
    @GetMapping("/change/{cid}")
    public String changeView(
            Model model, @PathVariable int cid){
    	
    	RKD rkd=rkdService.findByRKDId(cid);
    	String ZT=rkd.getZT();
    	if(ZT.equals("完成")==true){
    		 model.addAttribute("msg","出库单已完成，不能修改！");
    	        model.addAttribute("RKD",rkdService.findAll());
    	        return "RKD";
    	} 	
        model.addAttribute("cid",cid);
        return "RKDchange";
    }

}
